package com.maoye.ossp.biz.brand.controller;

import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.maoye.ossp.biz.brand.entity.BrandCate;
import com.maoye.ossp.biz.brand.entity.BrandReview;
import com.maoye.ossp.biz.brand.service.IBrandCateService;
import com.maoye.ossp.biz.brand.service.IBrandLevelService;
import com.maoye.ossp.biz.brand.service.IBrandReviewService;
import com.maoye.ossp.biz.brand.utils.BrandCommonConstant;
import com.maoye.ossp.biz.brand.utils.BrandUtils;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;

/**
 * 对象功能:品牌评审记录信息 控制器类
 * 开发公司:maoye shenzhen
 * 开发人员:harry
 * 创建时间:2013-09-30 11:37:20
 */
@Controller
@RequestMapping("/biz/brand/brandreview/")
public class BrandReviewController extends BaseController
{
	@Resource
	private IBrandReviewService brandReviewService;
	@Resource
	private IBrandLevelService brandLevelService;
	@Resource
	private IBrandCateService brandCateService;

	@RequestMapping(value = "save")
	public String save(BrandReview brandReview, Model model,
			RedirectAttributes redirectAttributes,HttpServletRequest request) 
	{
		try
		{
			String msg = brandReviewService.saveData(brandReview);
			if(null != msg && !"".equals(msg))
			{
				addMessage(model, msg);
				model.addAttribute("brandReview", brandReview);
				return "/biz/brand/brandreview/add";
			}
			addMessage(model, BrandCommonConstant.MSG_2002);
		}
		catch(Exception e)
		{
			addMessage(model, BrandCommonConstant.EX_1009);
		}
		
		return "redirect:/biz/brand/brandreview/list";
	}
	
	/**
	 * 品牌评审记录列表
	 * @param brandLevel
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "list")
	public String list(BrandReview brandReview, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		try {
			QueryFilter queryFilter = getQuerFilter(request, brandReview);
			Page<BrandReview> page = brandReviewService.find(queryFilter);
			model.addAttribute("page", page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultPage("/biz/brand/brandreview/list", model, request);
	}
	
	@RequestMapping(value="submit")
	public String submit( Model model,BrandReview brandReview,RedirectAttributes redirectAttributes,HttpServletRequest request,HttpServletResponse response) throws Exception {
		//Supplier getsupplier = supplierService.get(supplier.getId());
		brandReviewService.saveData(brandReview);
		BrandReview targetBrandReview=brandReviewService.getByBCIdAndReviewNo(brandReview.getBrandCateId(), brandReview.getReviewNo());
		brandReviewService.submit(targetBrandReview);
		addMessage(redirectAttributes, "提交'" + brandReview.getBrandCname()+ "品牌'成功");
		String navId = request.getParameter("navId");
		return "redirect:/biz/brand/brandcate/list?navId="+navId;
	}
	@RequestMapping(value = "add")
	public String add(BrandReview brandReview, Model model)  throws Exception 
	{
		return "/biz/brand/brandreview/add";
	}
	
	@RequestMapping(value = "{id}/getShopList")
	public void getShopList(@PathVariable String id,Model model, HttpServletRequest request,
			HttpServletResponse response) throws Exception 
	{
		String jsonStr = brandReviewService.getShopList(id);
        response.setCharacterEncoding("utf-8");
		response.setContentType("text/plain");
		PrintWriter out = response.getWriter();
		out.write(jsonStr);
	}
	
	@RequestMapping(value = "{id}/edit")
	public String edit(@PathVariable String id, Model model) throws Exception 
	{
		BrandReview brandReview = brandReviewService.get(Long.valueOf(id));
		model.addAttribute("brandReview", brandReview);
		return "/biz/brand/brandreview/add";
	}

	@RequestMapping(value = "{id}/view")
	public String view(@PathVariable String id, Model model) 
	{
		BrandReview brandReview = brandReviewService.get(Long.valueOf(id));
		model.addAttribute("brandReview", brandReview);
		return "/biz/brand/brandreview/view";
	}
	
	@RequestMapping(value = "{id}/delete")
	public String delete(@PathVariable String id, Model model,
			RedirectAttributes redirectAttributes) 
	{
		Long[] lAryId = {Long.valueOf(id)};
		brandReviewService.delAll(lAryId);
		addMessage(redirectAttributes, "删除数据成功！");
		return "redirect:/biz/brand/brandreview/list";
	}
	
	@RequestMapping(value = "deleteAll")
	public String deleteAll(HttpServletRequest request,
			HttpServletResponse response, Model model,RedirectAttributes redirectAttributes) 
	{
		String deleteIds = request.getParameter("deleteIds");
		Long[] lAryId = BrandUtils.getLongAryByStr(deleteIds,";");
		brandReviewService.delAll(lAryId);
		addMessage(redirectAttributes, "删除数据成功！");
		return "redirect:/biz/brand/brandreview/list";
	}
}
